<?php
// +----------------------------------------------------------------------
// | Author: 闪电 <573725206@qq.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;
use Common\Controller\AuthController;

class IndexController extends AuthController {
	//加载主页面
	public function index() {
		$this->Doughnut = self::Doughnut();
		$this->display();
	}
	//系统通知
	public function notice() {

		//添置按钮
		$array[] = array('title' => '添加消息', 'url' => U('addnotice'), 'class' => 'primary', 'type' => 'button', 'target' => 'myModal');
		$this->assign('add_button', add_button($array));

		$number = 32; //模型id
		//获取模型表单数据
		$this->FormHtml($number);
		//$type[] = array('title' => '已读', 'url' => 'state', 'class' => 'default', 'type' => 'delete');
		$type[] = array('title' => '编缉', 'url' => 'romnotice', 'class' => 'default', 'type' => 'rom');
		$type[] = array('title' => '删除', 'url' => 'del', 'class' => 'danger', 'type' => 'delete');
		$this->assign('array', $type);
		$this->assign('FormConfig', array('title' => '添加消息', 'url' => U('addnotice', array('mid' => $number))));
		//回滚处理执行函数
		$this->assign('rollback', $rollback = 'state');
		$this->display();
	}
	//添加消息
	public function addnotice() {

		if (IS_POST) {
			//保存询盘信息
			$this->PublicDataModel(I('get.mid'));
		}
	}
	//修改消息
	public function romnotice() {
		$mid = I('get.mid', '', 'intval');
		$id = I('get.id', '', 'intval');
		if (IS_POST) {
			$this->PublicDataModel($mid, $id, U('index'));
		} else {
			$this->PublicDataModel($mid, $id);
		}
	}
	public function del() {
		$mid = I('get.mid', '', 'intval');
		$id = I('get.id', '', 'intval');
		$data['id'] = $id;
		$delete = M(getModelTable($mid))->delete($id);
		if ($delete) {
			$this->success('删除成功');
		} else {
			$this->error('删除失败');
		}
	}
	//设置消息状态
	public function state() {
		$mid = I('get.mid', '', 'intval');
		$id = I('get.id', '', 'intval');
		$map['id'] = $id;

		//查出原始数据状态
		$info = M(getModelTable($mid))->where($map)->getField('browstuid');
		if (!$info) {
			//阅读记录不存在，即保存设置
			$json[] = getuserid();
			$data['browstuid'] = json_encode($json);
		} else {
			//存在记录
			$json = json_decode($info);
			if (in_array(getuserid(), $json)) {
				$this->error('您已阅读过！！！');
			} else {
				$json[] = getuserid();
				//全部用户阅读完，即设置状态为1
				$count = M('user')->where(array('lock' => 0))->count();
				if (count($json) == $count) {
					$data['state'] = 1;
				}
				$data['browstuid'] = json_encode($json);
			}

		}

		$state = M(getModelTable($mid))->where($map)->save($data);
		if ($state) {
			$this->success('成功设置状态');
		} else {
			$this->error('设置失败');
		}
	}
	//询单订单统计
	private static function Doughnut() {
		//未处理留言
		$a = M(getModelTable(47))->where($a_whree)->cache(true)->count();
		//友情链接
		$b = M(getModelTable(46))->where($b_whree)->cache(true)->count();
		//栏目数
		$c = M(getModelTable(41))->where($c_whree)->cache(true)->count();
		//单页数
		$d = M(getModelTable(45))->cache(true)->count();
		$json = array(
			array('label' => '留言', 'value' => $a, 'color' => '#F7464A'),
			array('label' => '友情链接', 'value' => $b, 'color' => '#E2EAE9'),
			array('label' => '栏目数', 'value' => $c, 'color' => '#D4CCC5'),
			array('label' => '单页数', 'value' => $d, 'color' => '#949FB1'),
		);
		return json_encode($json);
	}
}